<?php
	if ($_GET['page'] == "") {

		include("includes/session.php"); 
		include("includes/connect_db.php");
		include("includes/header.php"); 
		include("notif.php");
	?>

	<STYLE TYPE="text/css">
		.service{
			width: 100%;
			position: center;
			text-align: center;
			margin-top: 100px;
		}
		.btn{
			margin: auto;
			width: 250px;
			padding: 10px;
			border-radius: 10px;
		}
		.judul{
			text-align: center;
			padding-top: 200px;
			padding-bottom: 10px;
		}
	</STYLE>
	<div class="span9"> 
		
			<div class="span12 judul" style="text-align: center;">
				<h2>Service Status Gammu</h2>
			</div>
		<?php
			// menjalankan command untuk mengenerate file service.log
			passthru("net start > service.log");

			// membuka file service.log
			$handle = fopen("service.log", "r");

			// status awal = 0 (dianggap servicenya tidak jalan)
			$status = 0;

			// proses pembacaan isi file
			while (!feof($handle))
			{
			   // baca baris demi baris isi file
			   $baristeks = fgets($handle);
			   if (substr_count($baristeks, 'Gammu SMSD Service (GammuSMSD)') > 0)
			   {
			     // jika ditemukan baris yang berisi substring 'Gammu SMSD Service (GammuSMSD)'
			     // statusnya berubah menjadi 1
			     $status = 1;
			   }
			}
			// menutup file
			fclose($handle);

			// jika status terakhir = 1, maka gammu service running
			if ($status == 1) {
		?>
				<div class="service">
					<button class="status btn btn-success mati">
						<i class="icon-white icon-off icon-large"></i>
						Matikan Service
					</button>
				</div>
		<?php
			// jika status terakhir = 0, maka service gammu berhenti
			} else if ($status == 0) {
		?>
				<div class="service">
					<button class="status btn btn-danger hidup">
						<i class="icon-white icon-off icon-large"></i>
						Hidupkan Service
					</button>
				</div>
		<?php
			}
		?>
	</div>

	<SCRIPT TYPE="text/javascript">
		$(document).ready(function() {
			$('.mati').live('click', function(){
				$.ajax({
				  url: 'service.php?page=tes&aksi=1',
				  type: 'POST',
				  success: function(data, textStatus, xhr) {
					//window.location = 'service.php';
					$(".service").html("<button class='status btn btn-danger hidup'>"+
							"<i class='icon-white icon-off icon-large'></i>"+
							" Hidupkan Service"+
						"</button>");
				  }
				});
				return false;
			});

			$('.hidup').live('click', function(){
				$.ajax({
				  url: 'service.php?page=tes&aksi=2',
				  type: 'POST',
				  success: function(data, textStatus, xhr) {
					// window.location = 'service.php';
					$(".service").html("<button class='status btn btn-success mati'>"+
							"<i class='icon-white icon-off icon-large'></i>"+
							" Matikan Service"+
						"</button>");
				  }
				});
				return false;
			});
		});
	</SCRIPT>

	<?php include("includes/footer.php"); ?>

<?php 
	} elseif ($_GET[page]== "tes") {
		$aksi = $_GET['aksi'];

		if ($aksi == 1) {
			passthru('C:\Gammu\bin\gammu-smsd -k');
		} elseif ($aksi == 2) {
			passthru('C:\Gammu\bin\gammu-smsd -c smsdrc -s');
		}
	}
?>